using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using Common;
using Common.Enum;
using Common.NotifyEvent;

namespace SExperiment
{
    [Serializable]
    public class PatientInfo : NotifyPropertyChanged
    {
        private string _ID;

        private string _Name;

        private string _Sex;

        private string _Age;

        private string _Specimen;

        private string _SpecimenDate;

        private string _Doctor;

        private string _Office;

        private string _Diagnosis;

        private string _CaseID;

        private string _BedID;

        private string _InPatientID;

        private string _OutPatientID;

        private string _Remark;

        private ObservableCollection<string> _CustomData = new ObservableCollection<string>();

        public string ID
        {
            get
            {
                return _ID;
            }
            set
            {
                if (!(_ID == value))
                {
                    _ID = value;
                    OnPropertyChanged("ID");
                }
            }
        }

        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                if (!(_Name == value))
                {
                    _Name = value;
                    OnPropertyChanged("Name");
                }
            }
        }

        public string Sex
        {
            get
            {
                return _Sex;
            }
            set
            {
                if (!(_Sex == value))
                {
                    _Sex = value;
                    OnPropertyChanged("Sex");
                }
            }
        }

        public string Age
        {
            get
            {
                return _Age;
            }
            set
            {
                if (!(_Age == value))
                {
                    _Age = value;
                    OnPropertyChanged("Age");
                }
            }
        }

        public string Specimen
        {
            get
            {
                return _Specimen;
            }
            set
            {
                if (!(_Specimen == value))
                {
                    _Specimen = value;
                    OnPropertyChanged("Specimen");
                }
            }
        }

        public string SpecimenDate
        {
            get
            {
                return _SpecimenDate;
            }
            set
            {
                if (!(_SpecimenDate == value))
                {
                    _SpecimenDate = value;
                    OnPropertyChanged("SpecimenDate");
                }
            }
        }

        public string Doctor
        {
            get
            {
                return _Doctor;
            }
            set
            {
                if (!(_Doctor == value))
                {
                    _Doctor = value;
                    OnPropertyChanged("Doctor");
                }
            }
        }

        public string Office
        {
            get
            {
                return _Office;
            }
            set
            {
                if (!(_Office == value))
                {
                    _Office = value;
                    OnPropertyChanged("Office");
                }
            }
        }

        public string Diagnosis
        {
            get
            {
                return _Diagnosis;
            }
            set
            {
                if (!(_Diagnosis == value))
                {
                    _Diagnosis = value;
                    OnPropertyChanged("Diagnosis");
                }
            }
        }

        public string CaseID
        {
            get
            {
                return _CaseID;
            }
            set
            {
                if (!(_CaseID == value))
                {
                    _CaseID = value;
                    OnPropertyChanged("CaseID");
                }
            }
        }

        public string BedID
        {
            get
            {
                return _BedID;
            }
            set
            {
                if (!(_BedID == value))
                {
                    _BedID = value;
                    OnPropertyChanged("BedID");
                }
            }
        }

        public string InPatientID
        {
            get
            {
                return _InPatientID;
            }
            set
            {
                if (!(_InPatientID == value))
                {
                    _InPatientID = value;
                    OnPropertyChanged("InPatientID");
                }
            }
        }

        public string OutPatientID
        {
            get
            {
                return _OutPatientID;
            }
            set
            {
                if (!(_OutPatientID == value))
                {
                    _OutPatientID = value;
                    OnPropertyChanged("OutPatientID");
                }
            }
        }

        public string Remark
        {
            get
            {
                return _Remark;
            }
            set
            {
                if (!(_Remark == value))
                {
                    _Remark = value;
                    OnPropertyChanged("Remark");
                }
            }
        }

        public ObservableCollection<string> CustomData
        {
            get
            {
                return _CustomData;
            }
            set
            {
                if (_CustomData != value)
                {
                    _CustomData = value;
                    OnPropertyChanged("CustomData");
                }
            }
        }

        public PatientInfo(int customCount)
        {
            while (customCount-- > 0)
            {
                _CustomData.Add("");
            }
        }

        public void SaveToStream(StreamEx stream, EPatientInfoSaveType savePatientInfo = EPatientInfoSaveType.All)
        {
            stream.Write(PropertyKey.Key_PatientInfo);
            List<PropertyKey> list = new List<PropertyKey>();
            List<byte[]> list2 = new List<byte[]>();
            if (!string.IsNullOrEmpty(ID))
            {
                list.Add(PropertyKey.Key_ID);
                list2.Add(BytesConverter.GetBytes(ID));
            }
            if (savePatientInfo == EPatientInfoSaveType.All)
            {
                if (!string.IsNullOrEmpty(Name))
                {
                    list.Add(PropertyKey.Key_Name);
                    list2.Add(BytesConverter.GetBytes(Name));
                }
                if (!string.IsNullOrEmpty(Sex))
                {
                    list.Add(PropertyKey.Key_Sex);
                    list2.Add(BytesConverter.GetBytes(Sex));
                }
                if (!string.IsNullOrEmpty(Age))
                {
                    list.Add(PropertyKey.Key_Age);
                    list2.Add(BytesConverter.GetBytes(Age));
                }
                if (!string.IsNullOrEmpty(Specimen))
                {
                    list.Add(PropertyKey.Key_Specimen);
                    list2.Add(BytesConverter.GetBytes(Specimen));
                }
                if (!string.IsNullOrEmpty(SpecimenDate))
                {
                    list.Add(PropertyKey.Key_SpecimenDate);
                    list2.Add(BytesConverter.GetBytes(SpecimenDate));
                }
                if (!string.IsNullOrEmpty(Doctor))
                {
                    list.Add(PropertyKey.Key_Doctor);
                    list2.Add(BytesConverter.GetBytes(Doctor));
                }
                if (!string.IsNullOrEmpty(Office))
                {
                    list.Add(PropertyKey.Key_Office);
                    list2.Add(BytesConverter.GetBytes(Office));
                }
                if (!string.IsNullOrEmpty(Diagnosis))
                {
                    list.Add(PropertyKey.Key_Diagnosis);
                    list2.Add(BytesConverter.GetBytes(Diagnosis));
                }
                if (!string.IsNullOrEmpty(CaseID))
                {
                    list.Add(PropertyKey.Key_CaseID);
                    list2.Add(BytesConverter.GetBytes(CaseID));
                }
                if (!string.IsNullOrEmpty(BedID))
                {
                    list.Add(PropertyKey.Key_BedID);
                    list2.Add(BytesConverter.GetBytes(BedID));
                }
                if (!string.IsNullOrEmpty(InPatientID))
                {
                    list.Add(PropertyKey.Key_InPatientID);
                    list2.Add(BytesConverter.GetBytes(InPatientID));
                }
                if (!string.IsNullOrEmpty(OutPatientID))
                {
                    list.Add(PropertyKey.Key_OutPatientID);
                    list2.Add(BytesConverter.GetBytes(OutPatientID));
                }
                if (!string.IsNullOrEmpty(Remark))
                {
                    list.Add(PropertyKey.Key_Remark);
                    list2.Add(BytesConverter.GetBytes(Remark));
                }
                if (CustomData.Count > 0)
                {
                    list.Add(PropertyKey.Key_CustomData);
                    StreamEx streamEx = new StreamEx();
                    streamEx.Write(CustomData.Count);
                    foreach (string customDatum in CustomData)
                    {
                        streamEx.Write(customDatum);
                    }
                    list2.Add(streamEx.ToArray());
                    streamEx.Close();
                }
            }
            stream.Write(PropertyKey.Key_Index);
            stream.WriteIndex(list, list2);
            stream.Flush();
        }

        public void Clear()
        {
            ID = string.Empty;
            Name = string.Empty;
            Sex = string.Empty;
            Age = string.Empty;
            Specimen = string.Empty;
            SpecimenDate = string.Empty;
            Doctor = string.Empty;
            Office = string.Empty;
            Diagnosis = string.Empty;
            CaseID = string.Empty;
            BedID = string.Empty;
            InPatientID = string.Empty;
            OutPatientID = string.Empty;
            Remark = string.Empty;
            CustomData.Clear();
        }

        public void CopyTo(PatientInfo targetInfo)
        {
            targetInfo.Clear();
            StreamEx streamEx = new StreamEx();
            SaveToStream(streamEx);
            streamEx.Seek(0L, SeekOrigin.Begin);
            targetInfo.ReadFromStream(streamEx);
        }

        public bool ReadFromStream(StreamEx stream)
        {
            PropertyKey propertyKey = stream.ReadKey();
            if (propertyKey == PropertyKey.Key_PatientInfo)
            {
                propertyKey = stream.ReadKey();
                if (propertyKey == PropertyKey.Key_Index)
                {
                    List<PropertyKey> keys;
                    List<byte[]> dates;
                    stream.ReadIndex(out keys, out dates);
                    for (int i = 0; i < keys.Count; i++)
                    {
                        switch (keys[i])
                        {
                            case PropertyKey.Key_ID:
                                ID = BytesConverter.BytesToString(dates[i]);
                                break;
                            case PropertyKey.Key_Name:
                                Name = BytesConverter.BytesToString(dates[i]);
                                break;
                            case PropertyKey.Key_Sex:
                                Sex = BytesConverter.BytesToString(dates[i]);
                                break;
                            case PropertyKey.Key_Age:
                                Age = BytesConverter.BytesToString(dates[i]);
                                break;
                            case PropertyKey.Key_Specimen:
                                Specimen = BytesConverter.BytesToString(dates[i]);
                                break;
                            case PropertyKey.Key_SpecimenDate:
                                SpecimenDate = BytesConverter.BytesToString(dates[i]);
                                break;
                            case PropertyKey.Key_Doctor:
                                Doctor = BytesConverter.BytesToString(dates[i]);
                                break;
                            case PropertyKey.Key_Office:
                                Office = BytesConverter.BytesToString(dates[i]);
                                break;
                            case PropertyKey.Key_Diagnosis:
                                Diagnosis = BytesConverter.BytesToString(dates[i]);
                                break;
                            case PropertyKey.Key_CaseID:
                                CaseID = BytesConverter.BytesToString(dates[i]);
                                break;
                            case PropertyKey.Key_BedID:
                                BedID = BytesConverter.BytesToString(dates[i]);
                                break;
                            case PropertyKey.Key_InPatientID:
                                InPatientID = BytesConverter.BytesToString(dates[i]);
                                break;
                            case PropertyKey.Key_OutPatientID:
                                OutPatientID = BytesConverter.BytesToString(dates[i]);
                                break;
                            case PropertyKey.Key_Remark:
                                Remark = BytesConverter.BytesToString(dates[i]);
                                break;
                            case PropertyKey.Key_CustomData:
                                {
                                    StreamEx streamEx = new StreamEx();
                                    streamEx.Write(dates[i]);
                                    streamEx.Seek(0L, SeekOrigin.Begin);
                                    CustomData = new ObservableCollection<string>();
                                    int num = streamEx.ReadInt();
                                    for (int j = 0; j < num; j++)
                                    {
                                        CustomData.Add(streamEx.ReadString());
                                    }
                                    streamEx.Close();
                                    break;
                                }
                        }
                    }
                    return true;
                }
                stream.Seek(-4L, SeekOrigin.Current);
                return false;
            }
            stream.Seek(-4L, SeekOrigin.Current);
            while (true)
            {
                switch (stream.ReadKey())
                {
                    case PropertyKey.Key_ID:
                        ID = stream.ReadString();
                        break;
                    case PropertyKey.Key_Name:
                        Name = stream.ReadString();
                        break;
                    case PropertyKey.Key_Sex:
                        Sex = stream.ReadString();
                        break;
                    case PropertyKey.Key_Age:
                        Age = stream.ReadString();
                        break;
                    case PropertyKey.Key_Specimen:
                        Specimen = stream.ReadString();
                        break;
                    case PropertyKey.Key_SpecimenDate:
                        SpecimenDate = stream.ReadString();
                        break;
                    case PropertyKey.Key_Doctor:
                        Doctor = stream.ReadString();
                        break;
                    case PropertyKey.Key_Office:
                        Office = stream.ReadString();
                        break;
                    case PropertyKey.Key_Diagnosis:
                        Diagnosis = stream.ReadString();
                        break;
                    case PropertyKey.Key_CaseID:
                        CaseID = stream.ReadString();
                        break;
                    case PropertyKey.Key_BedID:
                        BedID = stream.ReadString();
                        break;
                    case PropertyKey.Key_InPatientID:
                        InPatientID = stream.ReadString();
                        break;
                    case PropertyKey.Key_OutPatientID:
                        OutPatientID = stream.ReadString();
                        break;
                    case PropertyKey.Key_Remark:
                        Remark = stream.ReadString();
                        break;
                    case PropertyKey.Key_CustomData:
                        {
                            CustomData = new ObservableCollection<string>();
                            int num2 = stream.ReadInt();
                            for (int k = 0; k < num2; k++)
                            {
                                CustomData.Add(stream.ReadString());
                            }
                            break;
                        }
                    default:
                        stream.Seek(-4L, SeekOrigin.Current);
                        return true;
                }
            }
        }
    }
}
